﻿/*************************************************************************
 *  Copyright (C), 2015-2016,
 *  FileName: StepManager.cs
 *  Author: xzy   Version: 1.0   Date: 2016/1/18
 *  Version Description:
 *    Step process control
 *************************************************************************/

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[System.Serializable]
public class step
{
    public StepProcess[] m_stepElement;
}


public class StepManager : MonoBehaviour
{
    public step[] m_allStep;

    [HideInInspector]
    public int m_curStep = 0;

    static bool[] m_boolStepArray;
    static int m_curStepIndex;

    bool[] m_allStepState;
    int m_stepCount;
    bool isFinished;
    static bool m_isWrong;//是否错过

    //出错的总次数
    public static int s_errorNum;
    //允许出错的次数
    public static int s_errorOk = 2;

    public delegate void StepReminder();
    public StepReminder StepRemin;

    // Use this for initialization
    void Start()
    {
        initStepState();
        //ErrorMarker = m_errorMarker;
        m_curStepIndex = 0;
        isFinished = true;
        m_isWrong = false;
    }

    void Update()
    {
        if (!m_allStepState[m_curStep])
        {
            RecordState();
        }
    }

    //初始化状态信息
    void initStepState()
    {
        m_stepCount = m_allStep.Length;
        //初始化答对的总数为0
        s_errorNum = 0;

        m_allStepState = new bool[m_stepCount];
        m_boolStepArray = new bool[m_stepCount];

        for (int i = 0; i < m_allStepState.Length; i++)
        {
            m_allStepState[i] = false;
            m_boolStepArray[i] = false;
        }

        for (int i = 0; i < m_allStep[0].m_stepElement.Length; i++)
        {
            m_allStep[0].m_stepElement[i].m_processExecutable = true;
        }

    }

    //修改文本框中的步骤提示
    void doNextStep()
    {
        //当前步骤完成，改变其执行状态
        for (int i = 0; i < m_allStep[m_curStep].m_stepElement.Length; i++)
        {
            m_allStep[m_curStep].m_stepElement[i].m_processExecutable = false;
        }

        m_curStep++;
        m_curStep = Mathf.Clamp(m_curStep, 0, m_stepCount - 1);

        for (int i = 0; i < m_allStep[m_curStep].m_stepElement.Length; i++)
        {
            m_allStep[m_curStep].m_stepElement[i].m_processFinished = false;
        }

        //下一步骤开启
        for (int i = 0; i < m_allStep[m_curStep].m_stepElement.Length; i++)
        {
            m_allStep[m_curStep].m_stepElement[i].m_processExecutable = true;
        }

        if (StepRemin != null)
        {
            StepRemin();
        }
        m_curStepIndex = m_curStep;
    }

    //修改步骤状态
    void RecordState()
    {
        //判断每一个步骤下面的小步骤是否全部完成
        for (int i = 0; i < m_allStep[m_curStep].m_stepElement.Length; i++)
        {
            if (!m_allStep[m_curStep].m_stepElement[i].m_processFinished)
            {
                //每一步下面得小步骤没有完成
                isFinished = false;
                return;
            }
            //每一小步全部完成
            isFinished = true;
        }
        //当前步骤完成
        if (isFinished)
        {
            if (!m_isWrong)
            {
                //每完成一步加分，成功次数
                s_errorNum++;
               // StaticData.s_scores += 100.0f / (m_stepCount - 1);
            }
            m_isWrong = false;

            m_allStepState[m_curStep] = true;

            //执行下一步操作的判断
            doNextStep();
            //每一步开始初始化出错次数为2，依次递减直到为0开始扣分
            s_errorOk = 2;
            isFinished = false;
        }
    }

    //错误提示以及扣分项
    public static void ErrorReminder()
    {
        //此处添加错误提示信息和弹框
       // ErrorMarker.PlayTween();
        //每一步允许出错两次
        if (!m_boolStepArray[m_curStepIndex])
        {
            s_errorOk--;
            s_errorOk = Mathf.Clamp(s_errorOk, 0, 2);
            if (s_errorOk == 0)
            {
                m_isWrong = true;
                m_boolStepArray[m_curStepIndex] = true;
                s_errorOk = 2;
            }
        }
    }
}
